package com.sree.tali.actions;
import com.sree.tali.buscomp.AdminBusComp;
import com.sree.tali.buscomp.AdminBusCompImpl;
import com.sree.tali.buscomp.ShowArticleBusComp;
import com.sree.tali.buscomp.ShowArticleBusCompImpl;
import com.sree.tali.common.ArtilceTypesConstants;
import com.sree.tali.common.CommonUtils;
import com.sree.tali.common.TalivudConstants;
import com.sree.tali.hibernate.pojo.IpPojo;
import com.sree.tali.hibernate.pojo.UserPojo;
import com.sree.tali.plugin.HibernatePlugin;
import com.sree.tali.pojo.ArticleTypes;
import com.sree.tali.pojo.HomeList;
import com.sree.tali.pojo.LoginForm;
import com.sree.tali.pojo.Role;
import com.sree.tali.pojo.RoleArticleMapping;
import com.sree.tali.pojo.RoleUserMapping;
import com.sree.tali.pojo.UploadImagePojo;
import com.sree.tali.util.SecurityAlgo;
import java.io.File;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.internal.SessionImpl;

public class UploadImages
  extends DispatchAction
{
  private static final String SUCCESS = "success";
  
  public ActionForward uploadImagePage(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws Exception
  {
    String basepath = getServlet().getServletContext().getRealPath("/");
    List<String> list = new ArrayList();
    UploadImagePojo uploadimagepojo = (UploadImagePojo)form;
    String[] children = new File(basepath).list();
    for (String subdir : children) {
      if (new File(getServlet().getServletContext().getRealPath("/") + subdir).isDirectory()) {
        list.add(subdir);
      }
    }
    uploadimagepojo.setFolders(list);
    SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
    LoginForm loginform = (LoginForm)request.getSession().getAttribute("loginform");
    uploadimagepojo.setArticlList(getArticleTypes(loginform.getJ_username(),localSessionFactory.openSession()));
    return mapping.findForward("uloadimages");
  }
  
  public ActionForward addImagePage(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws Exception
  {
    String basepath = getServlet().getServletContext().getRealPath("/");
    List<String> list = new ArrayList();
    UploadImagePojo uploadimagepojo = (UploadImagePojo)form;
    String[] children = new File(basepath).list();
    for (String subdir : children) {
      if (new File(getServlet().getServletContext().getRealPath("/") + subdir).isDirectory()) {
        list.add(subdir);
      }
    }
    uploadimagepojo.setFolders(list);
    SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
    LoginForm loginform = (LoginForm)request.getSession().getAttribute("loginform");
    uploadimagepojo.setArticlList(getArticleTypes(loginform.getJ_username(),localSessionFactory.openSession()));
    return mapping.findForward("addimages");
  }
  
  public ActionForward getSubfolders(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws Exception
  {
    UploadImagePojo uploadimagepojo = (UploadImagePojo)form;
    String basepath = getServlet().getServletContext().getRealPath("/") + uploadimagepojo.getFolderName();
    List<String> list = new ArrayList();
    String[] children = new File(basepath).list();
    for (String subdir : children) {
      if (new File(basepath + File.separator + subdir).isDirectory()) {
        list.add(subdir);
      }
    }
    uploadimagepojo.setSubFolders(list);
    return mapping.findForward("uloadimages");
  }
  
  public ActionForward getSubArticleTypes(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws Exception
  {
    UploadImagePojo uploadimagepojo = (UploadImagePojo)form;
    uploadimagepojo.setSubArticleList(getSubArticleTypes(uploadimagepojo.getArticleType()));
    return mapping.findForward("uloadimages");
  }
  
  public ActionForward uploadImages(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws Exception
  {
    AdminBusComp buscomp = new AdminBusCompImpl();
    UploadImagePojo localAdminHomepojo = (UploadImagePojo)form;
     CommonUtils localCommonUtils = new CommonUtils();
    SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
  //  String temp=localAdminHomepojo.getFolderName()+"/"+localAdminHomepojo.getSubfolderName()+"/"+localAdminHomepojo.getDirectoryname();
     String temp=localAdminHomepojo.getFolderName()+File.separator+localAdminHomepojo.getSubfolderName()+File.separator+localAdminHomepojo.getDirectoryname();
    System.out.println(temp);
    String path = temp.replace("/null", "").replaceAll("\\s+","");
    System.out.println(path);
    String str = getServlet().getServletContext().getRealPath(File.separator)+path;
    //localAdminHomepojo.setFilPath(str+"/"+localAdminHomepojo.getImage().getFileName());
    if(localAdminHomepojo.getImage()!=null) {
    localAdminHomepojo.setFilPath(path+File.separator+localAdminHomepojo.getImage().getFileName());
    }
     if(localAdminHomepojo.getArticleImage()!=null) {
    localAdminHomepojo.setImagePath(path+File.separator+localAdminHomepojo.getArticleImage().getFileName());
    }
    System.out.println(localAdminHomepojo.getFilPath());
    //int articleNumber = buscomp.getArticleNumber(localSessionFactory.openSession());
    localAdminHomepojo.setStatus("I");
    //localAdminHomepojo.setArticleNumber(articleNumber*10);
    DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
    Date date = new Date();
     String datestring = dateFormat.format(date);
     System.out.println("date string is -------------->" + datestring);
    localAdminHomepojo.setCreateddate(datestring);
    buscomp.UploadArticle(localAdminHomepojo, localSessionFactory.openSession());
    if(localAdminHomepojo.getImage()!=null) {
    localCommonUtils.UploadFile(localAdminHomepojo.getImage(), str);
    localCommonUtils.UploadFile(localAdminHomepojo.getArticleImage(), str);

    }
    return mapping.findForward("uloadimages");
  }
  
  public ActionForward addImages(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws Exception
  {
    AdminBusComp buscomp = new AdminBusCompImpl();
    UploadImagePojo localAdminHomepojo = (UploadImagePojo)form;
     CommonUtils localCommonUtils = new CommonUtils();
    //SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
   
  //  String temp=localAdminHomepojo.getFolderName()+"/"+localAdminHomepojo.getSubfolderName()+"/"+localAdminHomepojo.getDirectoryname();
     String temp=localAdminHomepojo.getFolderName()+File.separator+localAdminHomepojo.getSubfolderName()+File.separator+localAdminHomepojo.getDirectoryname();
    System.out.println(temp);
    String path = temp.replace("/null", "").replaceAll("\\s+","");
    System.out.println(path);
    String str = getServlet().getServletContext().getRealPath(File.separator)+path;
    //localAdminHomepojo.setFilPath(str+"/"+localAdminHomepojo.getImage().getFileName());
    localAdminHomepojo.setFilPath(path+File.separator+localAdminHomepojo.getImage().getFileName());
    System.out.println(localAdminHomepojo.getFilPath());
   // int articleNumber = buscomp.getArticleNumber(localSessionFactory.openSession());
   // localAdminHomepojo.setStatus("I");
    //localAdminHomepojo.setArticleNumber(articleNumber*10);
   // buscomp.UploadArticle(localAdminHomepojo, localSessionFactory.openSession());
    localCommonUtils.UploadFile(localAdminHomepojo.getImage(), str);
    if(localAdminHomepojo.getImage1()!=null)
            localCommonUtils.UploadFile(localAdminHomepojo.getImage1(), str);
    if(localAdminHomepojo.getImage2()!=null)
            localCommonUtils.UploadFile(localAdminHomepojo.getImage2(), str);
    if(localAdminHomepojo.getImage3()!=null)
            localCommonUtils.UploadFile(localAdminHomepojo.getImage3(), str);
    return mapping.findForward("addimages");
  }
  
  
  public ActionForward getSubfoldersForImages(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws Exception
  {
    UploadImagePojo uploadimagepojo = (UploadImagePojo)form;
    String basepath = getServlet().getServletContext().getRealPath(File.separator) + uploadimagepojo.getFolderName();
    List<String> list = new ArrayList();
    String[] children = new File(basepath).list();
    for (String subdir : children) {
      if (new File(basepath + File.separator + subdir).isDirectory()) {
        list.add(subdir);
      }
    }
    uploadimagepojo.setSubFolders(list);
    return mapping.findForward("addimages");
  }
  
  private List<String> getArticleTypes(String userName,Session session)
  //          private List<String> getArticleTypes()

  {
      List<String> articleTypesList = new ArrayList<String>();
        try
    {
      Connection localConnection = ((SessionImpl)session).connection();
      String qry= "select distinct ra.article_name from role_user ru , role_article ra where ru.role_name=ra.role_name and ru.user_name = '"+userName.trim()+"'";
     System.out.println(qry);
      Statement localStatement = localConnection.createStatement();
      ResultSet localResultSet = localStatement.executeQuery(qry.toUpperCase());
      while (localResultSet.next())
      {
        articleTypesList.add(localResultSet.getString(1));
      }
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
   
      return articleTypesList ;
      //return new ArtilceTypesConstants().getArticleTypes();
  }
  
  public List<String> getRolesForUser(String userName,Session session)
  //          private List<String> getArticleTypes()

  {
      List<String> userRolesList = new ArrayList<String>();
      //SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME); 
      // Session session = localSessionFactory.openSession();
       try
    {
      Connection localConnection = ((SessionImpl)session).connection();
      String qry= "select distinct ru.role_name from role_user ru  where  ru.user_name = '"+userName.trim()+"'";
      Statement localStatement = localConnection.createStatement();
      ResultSet localResultSet = localStatement.executeQuery(qry.toUpperCase());
      while (localResultSet.next())
      {
        userRolesList.add(localResultSet.getString(1));
      }
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
   
      return userRolesList ;
      //return new ArtilceTypesConstants().getArticleTypes();
  }
  
    private List<String> getSubArticleTypes(String articleType)
  {
      List<String> subArticleTypesList = new ArrayList<String>();
      SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME); 
      Session session = localSessionFactory.openSession();
      String qry= "from SubArticleTypes where articleType ='"+articleType+"'";
      Query query =session.createQuery(qry);
      List<ArticleTypes> articletypes = query.list();
      for(ArticleTypes type :articletypes )
      {
          subArticleTypesList.add(type.getArticleType());
      }
      return subArticleTypesList ;
  }
    
    
    public ActionForward viewArticlesPage(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws Exception
     {
         UploadImagePojo uploadimagepojo = (UploadImagePojo)form;
         SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
         LoginForm loginform = (LoginForm)request.getSession().getAttribute("loginform");
         System.out.println(getArticleTypes(loginform.getJ_username(),localSessionFactory.openSession()));
         uploadimagepojo.setArticlList(getArticleTypes(loginform.getJ_username(),localSessionFactory.openSession()));
         return mapping.findForward("viewarticlespage");   
     }
   
    
    public ActionForward viewArticles(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
      ShowArticleBusComp buscomp = new ShowArticleBusCompImpl();
      UploadImagePojo uploimagepojo = (UploadImagePojo)form;
      SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
      List<UploadImagePojo> viewarticles = buscomp.viewArticles(localSessionFactory.openSession(),uploimagepojo);
      System.out.println("articles size========="+viewarticles.size());
      request.getSession().setAttribute("viewarticles", viewarticles);
      return mapping.findForward("viewarticlespage");  
    }
 

 public ActionForward createUserPage(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
       if (!authorisedUser()){     return mapping.findForward("adduser");   }
        return mapping.findForward("adduser");   
    }
  private boolean authorisedUser()
  {
      return true;
  }
 

 public ActionForward createUser(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
      ShowArticleBusComp buscomp = new ShowArticleBusCompImpl();
      UploadImagePojo localAdminHomepojo = (UploadImagePojo)form;
      if(new SecurityAlgo().validatePassword(localAdminHomepojo.getPpassword(),TalivudConstants.ParentPassword))
      {
      SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
      buscomp.createUser( localSessionFactory.openSession(),localAdminHomepojo);
      }
      return mapping.findForward("adduser");  
    }
 

 public ActionForward addIpPage(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
        return mapping.findForward("addip");    
    }
 
 public ActionForward addIp(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
      UploadImagePojo localAdminHomepojo = (UploadImagePojo)form;
      ShowArticleBusComp buscomp = new ShowArticleBusCompImpl();
       if(new SecurityAlgo().validatePassword(localAdminHomepojo.getIppassword(),TalivudConstants.IpPassword))
      {
      SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
      buscomp.addIp(localSessionFactory.openSession(),localAdminHomepojo);
      }
      return mapping.findForward("addip");   
    }
 
   public ActionForward viewIncativeArticle(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
      ShowArticleBusComp buscomp = new ShowArticleBusCompImpl();
      SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
      List<UploadImagePojo> inactivearticles = buscomp.showInactiveArticle(localSessionFactory.openSession());
      request.setAttribute("inactivearticles", inactivearticles);
      return mapping.findForward("showarticles");  
    }
   
   public ActionForward updateArticle(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
      ShowArticleBusComp buscomp = new ShowArticleBusCompImpl();
      UploadImagePojo pojo = (UploadImagePojo)form;
      SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
      buscomp.updateArticle(localSessionFactory.openSession(), pojo ); 
      if(pojo.getImage()!=null) {
        CommonUtils localCommonUtils = new CommonUtils();
         String temp=pojo.getFolderName()+File.separator+pojo.getSubfolderName()+File.separator+pojo.getDirectoryname();
         System.out.println(temp);
         String path = temp.replace("/null", "").replaceAll("\\s+","");
         System.out.println(path);
         String str = getServlet().getServletContext().getRealPath(File.separator)+path;
        localCommonUtils.UploadFile(pojo.getImage(), str);
    }
        return mapping.findForward("viewarticlespage");  
    }
   
    public ActionForward editArticle(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
       ShowArticleBusComp buscomp = new ShowArticleBusCompImpl();
       int id  = Integer.parseInt(request.getParameter("id"));
       UploadImagePojo uploadimagepojo = (UploadImagePojo)form;
      SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
      UploadImagePojo uploadimagepojo1 = buscomp.editArticle(localSessionFactory.openSession(),id); 
      uploadimagepojo.setTitle(uploadimagepojo1.getTitle());
      uploadimagepojo.setDescription(uploadimagepojo1.getDescription());
      uploadimagepojo.setFolderName(uploadimagepojo1.getFolderName());
      uploadimagepojo.setSubfolderName(uploadimagepojo1.getSubfolderName());
      uploadimagepojo.setArticleType(uploadimagepojo1.getArticleType());
      uploadimagepojo.setArticleSubType(uploadimagepojo1.getArticleSubType());
      uploadimagepojo.setArticleNumber(uploadimagepojo1.getArticleNumber());
      uploadimagepojo.setId(uploadimagepojo1.getId());
      uploadimagepojo.setFilPath(uploadimagepojo1.getFilPath());
      uploadimagepojo.setStatus(uploadimagepojo1.getStatus());
      uploadimagepojo.setUrl(uploadimagepojo1.getUrl());
      uploadimagepojo.setImagePath(uploadimagepojo1.getImagePath());
     String basepath = getServlet().getServletContext().getRealPath(File.separator);
    List<String> list = new ArrayList();
    String[] children = new File(basepath).list();
    for (String subdir : children) {
      if (new File(getServlet().getServletContext().getRealPath(File.separator) + subdir).isDirectory()) {
        list.add(subdir);
      }
    }
    uploadimagepojo.setFolders(list);
    LoginForm loginform = (LoginForm)request.getSession().getAttribute("loginform");
    uploadimagepojo.setArticlList(getArticleTypes(loginform.getJ_username(),localSessionFactory.openSession()));
    String basepath1 = getServlet().getServletContext().getRealPath("/") + uploadimagepojo.getFolderName();
    List<String> list1 = new ArrayList();
    String[] children1 = new File(basepath1).list();
    for (String subdir : children1) {
      if (new File(basepath1 + File.separator + subdir).isDirectory()) {
        list1.add(subdir);
      }
    }
    uploadimagepojo.setSubFolders(list1);
    return mapping.findForward("updatearticle");
    
    }
   
   
     public ActionForward showIps( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
     {
      ShowArticleBusComp buscomp = new ShowArticleBusCompImpl();
      SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
      List<IpPojo> ippojolist = buscomp.showIps(localSessionFactory.openSession());
      System.out.print("user pojo list ============== "+ippojolist.size());
      request.setAttribute("ippojolist", ippojolist);
      return mapping.findForward("showips");  
     }
     public ActionForward showUsers( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
     {
      ShowArticleBusComp buscomp = new ShowArticleBusCompImpl();
      SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
      List<UserPojo> userpojolist = buscomp.showUsers(localSessionFactory.openSession());
      System.out.print("user pojo list ============== "+userpojolist.size());
      request.setAttribute("userpojolist", userpojolist);
      return mapping.findForward("showusers");  
     }
     public ActionForward inactivateUser(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
     {
       ShowArticleBusComp buscomp = new ShowArticleBusCompImpl();
       int id  = Integer.parseInt(request.getParameter("id"));
      SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
       buscomp.inactivateUser(localSessionFactory.openSession(),id);
       List<UserPojo> userpojolist = buscomp.showUsers(localSessionFactory.openSession());
      request.setAttribute("userpojolist", userpojolist);
          return mapping.findForward("showusers");  

     }
     public ActionForward inactivateIp(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
     {
         ShowArticleBusComp buscomp = new ShowArticleBusCompImpl();
         int id  = Integer.parseInt(request.getParameter("id"));
         SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
         buscomp.inactivateIp(localSessionFactory.openSession(),id);
         List<IpPojo> ippojolist = buscomp.showIps(localSessionFactory.openSession());
         request.setAttribute("ippojolist", ippojolist);
         return mapping.findForward("showips");  
     }
     public ActionForward deleteAllIps(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
     {
          ShowArticleBusComp buscomp = new ShowArticleBusCompImpl();
         SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
         buscomp.deleteAllIps(localSessionFactory.openSession());
         List<IpPojo> ippojo = buscomp.showIps(localSessionFactory.openSession());
         request.setAttribute("ippojo", ippojo);
          return mapping.findForward("showips");  
     }
     
      public ActionForward inactivateArticle(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
     {
       ShowArticleBusComp buscomp = new ShowArticleBusCompImpl();
       int id  = Integer.parseInt(request.getParameter("id"));
      SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
      buscomp.inactivateArticle(localSessionFactory.openSession(),id);
       return mapping.findForward("viewarticlespage");  

     }
       public ActionForward activateArticle(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
     {
       ShowArticleBusComp buscomp = new ShowArticleBusCompImpl();
       AdminBusComp adminbuscomp = new AdminBusCompImpl();
       int id  = Integer.parseInt(request.getParameter("id"));
      SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
      int articleNumber = adminbuscomp.getArticleNumber(localSessionFactory.openSession());
      //pojo.setArticleNumber(articleNumber*10);
      //buscomp.updateArticle(localSessionFactory.openSession(), pojo );
     
      buscomp.activateArticle(localSessionFactory.openSession(),id,articleNumber);
        return mapping.findForward("viewarticlespage");  

     }
       
         public ActionForward removeArticle(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
     {
       ShowArticleBusComp buscomp = new ShowArticleBusCompImpl();
       int id  = Integer.parseInt(request.getParameter("id"));
      SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
       buscomp.removeArticle(localSessionFactory.openSession(),id);
        return mapping.findForward("viewarticlespage");  

     }
           public ActionForward removeUser(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
     {
       ShowArticleBusComp buscomp = new ShowArticleBusCompImpl();
       int id  = Integer.parseInt(request.getParameter("id"));
      SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
       buscomp.removeUser(localSessionFactory.openSession(),id);
        return mapping.findForward("viewarticlespage");  

     }
             public ActionForward removeIp(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
     {
       ShowArticleBusComp buscomp = new ShowArticleBusCompImpl();
       int id  = Integer.parseInt(request.getParameter("id"));
      SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
       buscomp.removeUser(localSessionFactory.openSession(),id);
        return mapping.findForward("viewarticlespage");  

     }
       public ActionForward logout(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
     {
         LoginForm loginform = (LoginForm)request.getSession().getAttribute("loginform");
         loginform.setJ_password("");
         loginform.setJ_username("");
         request.getSession().removeAttribute("loginform");
         return mapping.findForward("adminloginpage");  

     }
     public ActionForward addArticleType(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
     {
        return mapping.findForward("addarticletypepage");  
     }
      public ActionForward saveArticleType(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
     {
      ShowArticleBusComp buscomp = new ShowArticleBusCompImpl();
      //int id  = Integer.parseInt(request.getParameter("id"));
      UploadImagePojo uploadimagepojo = (UploadImagePojo)form;
      SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
      buscomp.addArticleTypes(localSessionFactory.openSession(), uploadimagepojo);
      return mapping.findForward("addarticletypepage");  
     }
      public ActionForward viewArticleType(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
     {
         List<ArticleTypes> articletypes = new ArrayList<ArticleTypes>();
         ShowArticleBusComp buscomp = new ShowArticleBusCompImpl();
         SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
         articletypes = buscomp.viewArticleTypes(localSessionFactory.openSession());
         request.setAttribute("viewarticlestypes",articletypes);
         return mapping.findForward("viewarticletypepage");  
     }
        public ActionForward deleteArticleType(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
     {
         ShowArticleBusComp buscomp = new ShowArticleBusCompImpl();
         int id  = Integer.parseInt(request.getParameter("id"));
         SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
         buscomp.removeArticleTypes(localSessionFactory.openSession(), id);
         List<ArticleTypes> articletypes = new ArrayList<ArticleTypes>();
         articletypes = buscomp.viewArticleTypes(localSessionFactory.openSession());
         request.setAttribute("viewarticlestypes",articletypes);
        return mapping.findForward("viewarticletypepage");  
     }
     public ActionForward addRolePage(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
     {
        return mapping.findForward("addrolepage");  
     }
     public ActionForward addRoleUserPage(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
     {
          ShowArticleBusComp buscomp = new ShowArticleBusCompImpl();
         SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
         List<String> users = new ArrayList<String>();
         List<UserPojo> userpojos = new ArrayList<UserPojo>();
         List<String> roles = new ArrayList<String>();
         List<Role> rolespojos = new ArrayList<Role>();
         rolespojos =buscomp.viewRole(localSessionFactory.openSession());
         userpojos=buscomp.showUsers(localSessionFactory.openSession());
         for (UserPojo userpojo : userpojos) {
             users.add(userpojo.getUserName());
         }
         for (Role role : rolespojos) {
             roles.add(role.getRoleName());
         }
      //   request.getSession().setAttribute("users",users);
       //  request.getSession().setAttribute("roles",roles); 
          UploadImagePojo uploadimagepojo = (UploadImagePojo)form;
          uploadimagepojo.setRole_user_list(users);
          uploadimagepojo.setRole_role_list(roles);
         return mapping.findForward("addroleuserpage");  
     }
     public ActionForward addRoleArticlePage(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
     {
          ShowArticleBusComp buscomp = new ShowArticleBusCompImpl();
           UploadImagePojo uploadimagepojo = (UploadImagePojo)form;
         SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
         List<String> articletypes = new ArrayList<String>();
         List<ArticleTypes> articletypepojo = new ArrayList<ArticleTypes>();
         List<String> roles = new ArrayList<String>();
         articletypepojo=buscomp.viewArticleTypes(localSessionFactory.openSession());
         List<Role> rolespojos = new ArrayList<Role>();
         rolespojos =buscomp.viewRole(localSessionFactory.openSession());
      
         for (ArticleTypes atpojo : articletypepojo) {
             articletypes.add(atpojo.getArticleType());
         }
         for (Role role : rolespojos) {
             roles.add(role.getRoleName());
         }
       //  request.getSession().setAttribute("articletypes",articletypes);
        // request.getSession().setAttribute("roles",roles);
         uploadimagepojo.setRole_article_list(articletypes);
         uploadimagepojo.setRole_role_list(roles);
        return mapping.findForward("addrolearticlepage");  
     }
     public ActionForward addRole(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
     {
      ShowArticleBusComp buscomp = new ShowArticleBusCompImpl();
      //int id  = Integer.parseInt(request.getParameter("id"));
      UploadImagePojo uploadimagepojo = (UploadImagePojo)form;
      SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
      buscomp.addRole(localSessionFactory.openSession(), uploadimagepojo);
      return mapping.findForward("addrolepage");  
     }
      public ActionForward viewRoles(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
     {
         List<Role> roleslist = new ArrayList<Role>();
         ShowArticleBusComp buscomp = new ShowArticleBusCompImpl();
         SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
         roleslist = buscomp.viewRole(localSessionFactory.openSession());
         request.setAttribute("roleslist",roleslist);
         return mapping.findForward("viewroles");  
     }
        public ActionForward deleteRole(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
     {
         ShowArticleBusComp buscomp = new ShowArticleBusCompImpl();
         int id  = Integer.parseInt(request.getParameter("id"));
         SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
         buscomp.removeRole(localSessionFactory.openSession(), id);
         List<Role> roleslist = new ArrayList<Role>();
         roleslist = buscomp.viewRole(localSessionFactory.openSession());
         request.setAttribute("roleslist",roleslist);
         return mapping.findForward("viewroles");  
    }
         public ActionForward roleUserMapping(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
     {
         ShowArticleBusComp buscomp = new ShowArticleBusCompImpl();
         UploadImagePojo uploadimagepojo = (UploadImagePojo)form;
         SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
         buscomp.addRoleUser(localSessionFactory.openSession(), uploadimagepojo);
         return mapping.findForward("addroleuserpage");  
     }
         
          public ActionForward viewRoleUserMapping(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
     {
         List<RoleUserMapping> roleuserlist = new ArrayList<RoleUserMapping>();
         ShowArticleBusComp buscomp = new ShowArticleBusCompImpl();
         SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
         roleuserlist = buscomp.viewRoleUser(localSessionFactory.openSession());
         request.setAttribute("roleuserlist",roleuserlist);
         return mapping.findForward("viewroleusers");  
     }
                public ActionForward deleteRoleUserMapping(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
     {
           ShowArticleBusComp buscomp = new ShowArticleBusCompImpl();
         int id  = Integer.parseInt(request.getParameter("id"));
         SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
         buscomp.removeRoleUser(localSessionFactory.openSession(), id);
         List<RoleUserMapping> roleuserlist = new ArrayList<RoleUserMapping>();
         roleuserlist = buscomp.viewRoleUser(localSessionFactory.openSession());
         request.setAttribute("roleuserlist",roleuserlist);
         return mapping.findForward("viewroleusers");  
     }
           public ActionForward roleArticleMapping(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
     {
          ShowArticleBusComp buscomp = new ShowArticleBusCompImpl();
         UploadImagePojo uploadimagepojo = (UploadImagePojo)form;
         SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
         buscomp.addRoleArticle(localSessionFactory.openSession(), uploadimagepojo);
         return mapping.findForward("addrolearticlepage");  
     }
            public ActionForward ViewRoleArticleMapping(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
     {
         List<RoleArticleMapping> rolearticlelist = new ArrayList<RoleArticleMapping>();
         ShowArticleBusComp buscomp = new ShowArticleBusCompImpl();
         SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
         rolearticlelist = buscomp.viewRoleArticle(localSessionFactory.openSession());
         request.setAttribute("rolearticlelist",rolearticlelist);
         return mapping.findForward("viewrolearticles");  
     }
          public ActionForward deleteRoleArticleMapping(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
     {
           ShowArticleBusComp buscomp = new ShowArticleBusCompImpl();
         int id  = Integer.parseInt(request.getParameter("id"));
         SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
         buscomp.removeRoleArticle(localSessionFactory.openSession(), id);
         List<RoleArticleMapping> rolearticlelist = new ArrayList<RoleArticleMapping>();
         rolearticlelist = buscomp.viewRoleArticle(localSessionFactory.openSession());
         request.setAttribute("rolearticlelist",rolearticlelist);
         return mapping.findForward("viewrolearticles");  
     }
}



